package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.order.pojo.OrderItem;
import com.changgou.user.feign.UserInfoFeign;
import com.changgou.user.pojo.Address;
import jdk.net.SocketFlow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/worder")
public class WOrderController {

    @Autowired
    private UserInfoFeign userInfoFeign;
     @Autowired
     private CartFeign cartFeign;
     @Autowired
     private OrderFeign orderFeign;
    @GetMapping("/ready/order")
    public String orderlist(Model model){
        Result<List<Address>> byUsername = userInfoFeign.findByUsername();
        List<Address> addressList = byUsername.getData();
        model.addAttribute("address",addressList);
        Map map = cartFeign.list();
        List<OrderItem> orderItemList=(List<OrderItem>)map.get("orderItemList");

        Integer totalMoney =(Integer) map.get("totalPrice");
        Integer totalNum =(Integer) map.get("totalNum");
        model.addAttribute("carts",orderItemList);
        model.addAttribute("totalPrice",totalMoney);
        model.addAttribute("totalNum",totalNum);
        for (Address address : addressList) {
            if ("1".equals(address.getIsDefault())){
                //默认收件人
                model.addAttribute("deAddr",address);
                break;
            }
        }
        return "order";
    }
    @PostMapping(value = "/add")
    @ResponseBody
    public Result add(@RequestBody Order order){
        Result add = orderFeign.add(order);
        return add;
    }


}
